import 'dart:io';

import 'package:bot_toast/bot_toast.dart';
import 'package:crop_image_project/image_processor.dart';
import 'package:crop_image_project/loading_view_utils.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      builder: BotToastInit(),
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      navigatorObservers: [BotToastNavigatorObserver()],
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String? maskPath;
  String? originPath;
  String? processedImagePath;

  @override
  void initState() {
    super.initState();
    maskPath = imagePath("mask.jpeg");
    originPath = imagePath("origin.png");
  }

  ///本地图片资源文件路径
  String imagePath(String pathName) {
    return "assets/images/$pathName";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("裁剪Demo", style: TextStyle(color: Colors.black, fontSize: 16))),
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Image.asset(originPath!, height: 150, width: 150, fit: BoxFit.contain),
              Container(height: 10),
              Image.asset(maskPath!, height: 150, width: 150, fit: BoxFit.contain),
              Container(height: 10),
              processedImagePath == null ? Container() : Image.file(File(processedImagePath!), height: 150, width: 150, fit: BoxFit.contain),
              Container(height: 10),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  InkWell(
                    child: Container(
                      alignment: Alignment.center,
                      child: Text("裁剪", style: TextStyle(color: Colors.white, fontSize: 15)),
                      width: 100,
                      height: 50,
                      decoration: BoxDecoration(color: Colors.blue, borderRadius: BorderRadius.circular(5)),
                    ),
                    onTap: () {
                      LoadingViewUtils.getInstance().showLoadingView();
                      ImageProcessor.processImages(maskPath!, originPath!).then((value) {
                        setState(() {
                          LoadingViewUtils.getInstance().dismissLoadingView();
                          processedImagePath = value;
                        });
                      });
                    },
                  ),
                  Container(width: 20),
                  InkWell(
                    child: Container(
                      alignment: Alignment.center,
                      child: Text("撤销", style: TextStyle(color: Colors.white, fontSize: 15)),
                      width: 100,
                      height: 50,
                      decoration: BoxDecoration(color: Colors.blue, borderRadius: BorderRadius.circular(5)),
                    ),
                    onTap: () {
                      ImageProcessor.processImages(maskPath!, originPath!).then((value) {
                        setState(() {
                          processedImagePath = null;
                        });
                      });
                    },
                  )
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}
